<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Redefining message() - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../experimental/worked-example/string_ref.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/worked-example/source.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Redefining <code>message()</code></h1></div></div></div>
  <p>You may remember that our custom <code>_file_io_error_domain</code> inherits from
<code>outcome_e::posix_code::domain_type</code>, and thus does not have to
implement the many pure virtual functions required by <code>outcome_e::status_code_domain</code>.</p>

<p>What we do need to do is reimplement <code>_do_message()</code> to append the
file and line information to the POSIX error description string
returned by <code>outcome_e::posix_code::domain_type</code>. This causes
the status code&rsquo;s <code>.message()</code> observer to return a string
with the extra payload information represented in text.</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++">  <span class="c1">// Return a string describing a specific code. We will return the
</span><span class="c1"></span>  <span class="c1">// string returned by our POSIX code base domain, with the source
</span><span class="c1"></span>  <span class="c1">// file and line number appended
</span><span class="c1"></span>  <span class="k">virtual</span> <span class="n">_base</span><span class="o">::</span><span class="n">string_ref</span> <span class="n">_do_message</span><span class="p">(</span><span class="k">const</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">status_code</span><span class="o">&lt;</span><span class="kt">void</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">code</span><span class="p">)</span> <span class="k">const</span> <span class="k">noexcept</span> <span class="k">override</span> <span class="k">final</span>  <span class="c1">// NOLINT
</span><span class="c1"></span>  <span class="p">{</span>
    <span class="n">assert</span><span class="p">(</span><span class="n">code</span><span class="p">.</span><span class="n">domain</span><span class="p">()</span> <span class="o">==</span> <span class="o">*</span><span class="k">this</span><span class="p">);</span>

    <span class="c1">// Fetch message from base domain (POSIX)
</span><span class="c1"></span>    <span class="k">auto</span> <span class="n">msg</span> <span class="o">=</span> <span class="n">_base</span><span class="o">::</span><span class="n">_do_message</span><span class="p">(</span><span class="n">code</span><span class="p">);</span>
    <span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="n">c1</span> <span class="o">=</span> <span class="k">static_cast</span><span class="o">&lt;</span><span class="k">const</span> <span class="n">file_io_error</span> <span class="o">&amp;&gt;</span><span class="p">(</span><span class="n">code</span><span class="p">);</span>  <span class="c1">// NOLINT
</span><span class="c1"></span>    <span class="k">const</span> <span class="n">value_type</span> <span class="o">&amp;</span><span class="n">v</span> <span class="o">=</span> <span class="n">c1</span><span class="p">.</span><span class="n">value</span><span class="p">();</span>

    <span class="c1">// Append my source file and line number
</span><span class="c1"></span>    <span class="k">if</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">file</span> <span class="o">==</span> <span class="k">nullptr</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="k">return</span> <span class="n">msg</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">size_t</span> <span class="n">length</span> <span class="o">=</span> <span class="n">strlen</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">file</span><span class="p">)</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="n">msg</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
    <span class="k">auto</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="k">static_cast</span><span class="o">&lt;</span><span class="kt">char</span> <span class="o">*&gt;</span><span class="p">(</span><span class="n">malloc</span><span class="p">(</span><span class="n">length</span><span class="p">));</span>  <span class="c1">// NOLINT
</span><span class="c1"></span>    <span class="k">if</span><span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="k">nullptr</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="k">return</span> <span class="n">_base</span><span class="o">::</span><span class="n">string_ref</span><span class="p">(</span><span class="s">&#34;failed to get message from system&#34;</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="n">sprintf</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="s">&#34;%s (%s:%d)&#34;</span><span class="p">,</span> <span class="n">msg</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">v</span><span class="p">.</span><span class="n">file</span><span class="p">,</span> <span class="n">v</span><span class="p">.</span><span class="n">lineno</span><span class="p">);</span>

    <span class="c1">// Return as atomically reference counted string
</span><span class="c1"></span>    <span class="k">return</span> <span class="n">_base</span><span class="o">::</span><span class="n">atomic_refcounted_string_ref</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">length</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">};</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/experimental_status_code.cpp#L111" class="code-snippet-url" target="_blank">View this code on Github</a></div>



        </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../experimental/worked-example/string_ref.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/worked-example/source.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>
